# Syntax idea: \`like\` instead of \`similar\`

**URL:** https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456
**Category:** Internals & Design
**Created:** [November 20, 2016, 10:40pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456 "2016-11-20T22:40:21Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![pitao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitao/32/116_2.png) [@pitao](https://discourse.julialang.org/u/pitao)
#### Post date: [November 20, 2016, 10:40pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/1 "2016-11-20T22:40:21Z")

</div>

`rand(like!(M))` would read better than the current `rand(similar(M))`

It wins by 3 letters and 2 syllables.

EDIT: (thanks @fengyang.wang)  
EDIT: how about `shape`? I think that’s better than either `similar` or `like` (see [post #10](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/10)).

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [November 21, 2016, 12:33am UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/2 "2016-11-21T00:33:24Z")

</div>

You probably mean `rand!(like(M))`.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 21, 2016, 2:40am UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/3 "2016-11-21T02:40:10Z")

</div>

> [@pitao](#):
>
> It wins by 3 letters and 2 syllables.

It’s also closer to `ones` and `zeros` which are arguably its cousin.

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [November 21, 2016, 9:20am UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/4 "2016-11-21T09:20:20Z")

</div>

> [@ChrisRackauckas](#):
>
> It’s also closer to ones and zeros which are arguably its cousin.

Just curious: why you find it closer?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 21, 2016, 9:23am UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/5 "2016-11-21T09:23:50Z")

</div>

Length. `like`, `size`, `ones`, `zeros` are all similar in length and tend to be used for similar things. The word `similar` doesn’t seem to fit in. (This whole response is a mouthful).

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [November 21, 2016, 12:28pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/6 "2016-11-21T12:28:21Z")

</div>

Couldn’t this just be a method of copy, maybe with a keyword argument to say to copy only the structure, not the entries?

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [November 21, 2016, 1:12pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/7 "2016-11-21T13:12:54Z")

</div>

What problem are we trying to solve here?

---

<div class="post-metadata">

### Author: ![pitao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitao/32/116_2.png) [@pitao](https://discourse.julialang.org/u/pitao)
#### Post date: [November 21, 2016, 1:20pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/8 "2016-11-21T13:20:18Z")

</div>

My original suggestion was simply to replace `similar` with `like`, as it is a better choice of keyword:

- shorter to write – 4 chars not 7
- shorter to (sub)vocalize – 1 syllable not 3
- reads better – `x like y` as opposed to `x similar TO y`, i.e. `TO` is required to make decent English, `x similar y` grates.

So it’s not really “solving a problem” per se. Unless the problem is “How to go about constructing the most elegant possible language?” in which case I think it is a minute step in the right direction.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 21, 2016, 3:04pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/9 "2016-11-21T15:04:21Z")

</div>

> [@nalimilan](#):
>
> maybe with a keyword argument to say to copy only the structure, not

I’m not saying it’s enough to act on it, but I do like it. But as @dpsanders noted, it would probably be most clear to just have it as a variant of copy.

---

<div class="post-metadata">

### Author: ![pitao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitao/32/116_2.png) [@pitao](https://discourse.julialang.org/u/pitao)
#### Post date: [November 21, 2016, 3:10pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/10 "2016-11-21T15:10:23Z")

</div>

Then how about `shape` if we are just trying to copy the structure?

```julia
rand!(shape(M))

```

… that actually makes linguistic sense. i.e. You could understand that without prior Julia knowledge, whereas both `like` and `similar` are both misleading.

i.e. `rand!(like(M))` may make sense, but `like(M)` on its own doesn’t make sense. Because the thing it returns isn’t like `M`, it is the _shape_ of `M`.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [November 21, 2016, 7:06pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/11 "2016-11-21T19:06:10Z")

</div>

`rand(size(M))` currently works just fine for this; it’ll default to Float64 elements but you can easily specify the element type you want with `rand(T, size(M))`.

The biggest critique of `similar` also applies to the name `like` — it doesn’t really specify what parts of the “structure” are important and precisely in what regard it should be similar to the original array. At least some elements need to be mutable for the returned thing to be useful, but do all of them need to be? What type should be returned if the original array type doesn’t support the given shape? You’re really asking the original array to guess what data structure will be most efficient or most useful in the subsequent operations… which of course it cannot know.

Honestly, I don’t think `similar` is all that useful when you’re writing non-library code. It’s typically simpler, more readable, and more efficient (in terms of the subsequent complexity) to just be explicit and ask for `Array{T}(sz)` or use construct like `rand(size(M))` above.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 21, 2016, 8:00pm UTC](https://discourse.julialang.org/t/syntax-idea-like-instead-of-similar/456/12 "2016-11-21T20:00:31Z")

</div>

> [@mbauman](#):
>
> rand(size(M)) currently works just fine for this; it’ll default to Float64 elements but you can easily specify the element type you want with rand(T, size(M)).

No it doesn’t work for this. That will create an `Array` with that size. Not every `AbstractArray` is an `Array`.
